var ItemCode = $('input[name="Code"]'),
    ItemName = $('input[name="Name"]'),
    ItemLevel = $('input[name="Level"]'),
    ItemPCode = $('input[name="PCode"]'),
    mCode = $('#m_Code'),
    mName = $('#m_Name'),
    mLevel = $('#m_Level'),
    mPCode = $('#m_PCode'),
    tree = $('#jstree_folders');

var Data = [],
    Code = '',
    Name = '';

$(function () {
    treeInit();
    requestAjax01();
    $('li[data-opt="edit"]').on('click', nodeEdit);
    $('li[data-opt="delete"]').on('click', nodeDelete);
    $('li[data-opt="new1"]').on('click', nodeNew1);
    $('li[data-opt="new2"]').on('click', nodeNew2);
    tree.on("changed.jstree", function (e, data) {
        //console.log(data.action) //deselect_all,select_node,ready
        if (data.action === 'select_node') {
            //console.log('选中节点')
            Code = data.node.li_attr.Code;
            Name = data.node.li_attr.Name;
            requestAjax02();
        }else if(data.action === 'deselect_all') {
            //console.log('加载完成')
            var Selected = $('li[code="MainPageClass"]');
            Selected.find('>a').trigger('click');
        }
    });
    $('.modal-data').draggable();
});

function context_menu(){
    var items = {
        'Edit': {
            'label': '编辑',
            'action': nodeEdit
        },
        'Delete': {
            'label': '删除',
            'action': nodeDelete
        },
        'New1': {
            'label': '新建同级',
            'action': nodeNew1
        },
        'New2': {
            'label': '新建子级',
            'action': nodeNew2
        }
    };
    return items;
}

function requestAjax01(){   //MDM001902
    $.ajax({
        async: false,
        type: "post",
        url: mdmPath + "/MDM0019/MDM001902",
        data: {
            Method: 'Q',
            Tonken: postData.Tonken,
            UserCode: postData.UserCode,
            BrowserID: postData.BrowserID,
            Source: postData.Source
        },
        dataType: "json",
        success: function(re) {
            loadAjax01(re);
        }
    });
}
function loadAjax01(re){
    if (re.Status == -999) {
        alert_QX($('body'), re.Message);
    } 
    Data = re.Content;
    //Data = [];
    if(Data.length>0){
        treeRefresh();    
    }else{
        firstNodeNew();
    }
}

function requestAjax02(){   //MDM001903
    var postData = {
        Method: 'Q',
        Content: {
            Code: Code
        }
    }
    for(i in params){
        postData[i] = params[i];
    }
    $.ajax({
        async: false,
        type: "post",
        url: mdmPath + "/MDM0019/MDM001903",
        data: postData,
        dataType: "json",
        success: function(re) {
            loadAjax02(re);
        }
    });
}
function loadAjax02(re){
    if (re.Status == 200) {
        ItemCode.val(re.Content.Code);
        ItemName.val(re.Content.Name);
        ItemLevel.val(re.Content.Level);
        ItemPCode.val(re.Content.PCode);
    } else if (re.Status == -999) {
        alert_situ($('.u_row').find('.form-horizontal'),re.Message);
    }else {
        alert(re.Message);
    }
}

// options
function nodeNew1(){
	if(Code){
		mPCode.val(ItemPCode.val());
		mLevel.val(ItemLevel.val());
		mCode.val('').attr('disabled', false);
		mName.val('');
		$('.modal-overlay').removeClass('hidden');
		$('#submitBtn').unbind('click').bind('click', function(){
			var _postData = {
				Method: 'A'
			}
			requestAjax03(_postData);
		})
	}else{
		myAlert('请选择您要编辑的数据');
	}
}
function nodeNew2(){
	if(Code){
		mPCode.val(ItemCode.val());
		mLevel.val(parseInt(ItemLevel.val())+1);
		mCode.val('').attr('disabled', false);
		mName.val('');
		$('.modal-overlay').removeClass('hidden');
		$('#submitBtn').unbind('click').bind('click', function(){
			var _postData = {
				Method: 'A'
			}
			requestAjax03(_postData);
		})
	}else{
		myAlert('请选择您要编辑的数据');
	}
}
function firstNodeNew(){
    $('.u_fixed_row').append('<a href="javascript:0" class="firstBtn">开始创建第一条数据</a>');
    $('.firstBtn').on('click', function(){
        mPCode.val(0);
        mLevel.val(1);
        mCode.val('').attr('disabled', false);
        mName.val('');
        $('.modal-overlay').removeClass('hidden');
            $('#submitBtn').unbind('click').bind('click', function(){
            var _postData = {
                Method: 'A'
            }
            requestAjax03(_postData);
            $('.firstBtn').remove();
        })
    })
}
function nodeEdit(){
	if(Code){
		mPCode.val(ItemPCode.val());
		mLevel.val(ItemLevel.val());
		mCode.val(ItemCode.val()).attr('disabled', true);
		mName.val(ItemName.val());
		$('.modal-overlay').removeClass('hidden');
		$('#submitBtn').unbind('click').bind('click', function(){
			var _postData = {
				Method: 'U'
			}
			requestAjax03(_postData);
		})
	}else{
		myAlert('请选择您要编辑的数据');
	}
}
function nodeDelete(){
	if(Code){
    	console.log(ItemCode.val())
	}else{
		myAlert('请选择您要删除的数据');
	}
}
function requestAjax03(_postData){  //MDM001901
    _postData.Content = {
        Code: mCode.val(),
        Name: mName.val(),
        Level: mLevel.val(),
        PCode: mPCode.val()
    };
    for(i in postData){
        _postData[i] = postData[i];
    };
    console.log(_postData)
    $.ajax({
        type: "post",
        url: mdmPath + "/MDM0019/MDM001901",
        data: _postData,
        dataType: "json",
        success: function(re) {
            loadAjax03(re);
        }
    });
}
function loadAjax03(re){
    if (re.Status == 200) {
        requestAjax01();
        $('.modal-overlay').addClass('hidden');
    }else{
        alert(re.Message);
    }
}
